var express = require('express');
var mysql = require('../db/mysqlDB.js');
var path = require('path')
var fs = require('fs');
var fm = require('formidable'); //处理文件上传
var router = express.Router();

router.post('/login', function(req, res, next) {
	//console.log(req.query.schoolId);
	var me = req.body;
	mysql.getList("schoolId", me.schoolId, "users", function(result) {
		if (result.length) { //存在用户
			res.cookie('login', {
				id: result[0].schoolId
			}, {
				maxAge: 1000 * 60 * 100
			}); //记录cookies,每100分钟跟新
			res.locals.result = '成功';
			//console.log(result);
			res.json(result);
		} else {
			mysql.insertUser(me, function(result) {
				//console.log('插入用户成功');
				res.cookie('login', {
					id: me.schoolId
				}, {
					maxAge: 1000 * 60 * 100
				}); //记录cookies,每100分钟跟新
				res.locals.result = '成功';
				res.json(result);
			});
		}
	});
});
router.post('/info', function(req, res, next) {
	mysql.getList("schoolId", req.body.id, "users", function(result) {
		res.json(result);
	});
});
router.post('/mine', function(req, res, next) {
	mysql.getList("schoolId", req.body.schoolId, "users", function(result) {
		res.json(result);
	});
});
router.post('/info/headimg', function(req, res, next) {
	var form = new fm.IncomingForm();
	// 5.2设置路径
	// 注：把路径设置为静态路径下的uploads，需在public下创建uploads
	form.uploadDir = path.join(__dirname, '../files/user')
		// uploadDir设置文件的上传的路径
		// 5.3解析上传内容
	form.parse(req);
	// 5.4 监听end事件，判断是否上传结束
	form.on('end', function() {
			//console.log('upload success')
		})
		// 5.5监听file事件(在服务器的路径下，有上传的文件)，处理上传内容
	form.on('file', function(field, file) { //file是上传的文件
		// 5.5.1 更改上传文件的名字
		// 使用同步更改
		//console.log(file.newname);
		fs.renameSync(file.path, path.join(form.uploadDir, file.name));
		// 第一个参数file.path表示上传的文件所在的路径
		// 5.5.2发送给浏览器端(客户端)
		mysql.changeheaderimg('/files/user/' + file.name, req.cookies.login.id, function() {
			res.send('/files/user/' + file.name);
		})
	})
});
router.post('/info/changeinfo', function(req, res, next) {
	mysql.changeinfo(req.body.name, req.body.sign, req.cookies.login.id, function() {
		res.json({
			name: req.body.name,
			sign: req.body.sign
		});
	})
});
router.post('/getres', function(req, res, next) {
	mysql.getAllres(function(result) {
		res.json(result);
	})
});
router.post('/relation', function(req, res, next) {
	mysql.getrelation('schoolId', req.body.userid, 'relation', req.body.relation, function(result) {
		if(result.length){
			res.json(result);
		}else{
			res.json({});
		}
	})
});
router.post('/uploadfile', function(req, res, next) {
	var form = new fm.IncomingForm();
	// 5.2设置路径
	// 注：把路径设置为静态路径下的uploads，需在public下创建uploads
	form.uploadDir = path.join(__dirname, '../files/resource')
		// uploadDir设置文件的上传的路径
		// 5.3解析上传内容
	form.parse(req);
	// 5.4 监听end事件，判断是否上传结束
	form.on('end', function() {
			//console.log('upload success')
		})
		// 5.5监听file事件(在服务器的路径下，有上传的文件)，处理上传内容
	form.on('file', function(field, file) { //file是上传的文件
		// 5.5.1 更改上传文件的名字
		// 使用同步更改
		//console.log(file.newname);
		fs.renameSync(file.path, path.join(form.uploadDir, file.name));
		// 第一个参数file.path表示上传的文件所在的路径
		// 5.5.2发送给浏览器端(客户端)
		//console.log('http://ow365.cn/?i=14260&furl=http://47.95.218.7:3000/files/resource/'+file.name);
		mysql.insertRes('http://ow365.cn/?i=14260&furl=http://47.95.218.7:3000/files/resource/'+file.name,function(result){
			//console.log(result)
			res.json(result.insertId);
		});
	})
});
router.post('/changefileinfo',function(req,res,next){
	mysql.changefileinfo(req.body,function(result){
		mysql.addUpload(req.body.schoolid,function(result){
		});
		mysql.addRelation(req.body.schoolid,req.body.id,2,function(result){
			res.json(req.body.id);
		})
	});
});
router.post('/download',function(req,res,next){
	mysql.addRelation(req.body.schoolid,req.body.id,1,function(){
	});
	mysql.andDownload(req.body.id,function(){
	});
	mysql.addDownload(req.body.schoolid,function(){
	});
	mysql.getResurl(req.body.id,function(result){
		res.json(result);
	});
});
router.post('/view', function(req, res, next) {
	mysql.addRelation(req.body.id,req.body.resid,3,function(){
	});
	mysql.andRead(req.body.resid,function(){		
	});
	mysql.isLike(req.body.id,req.body.resid,function(result){
		if(result.length>0){
			res.json(result);
		}
	});
});
router.post('/feedback',function(req,res,next){
	mysql.insertFeedback(req.body,function(result){
		res.json(result);
	});
});
router.post('/likeit',function(req,res,next){
	mysql.addRelation(req.body.schoolid,req.body.id,0,function(){
	});
	mysql.addLike(req.body.schoolid,function(){
		//console.log('成功');
	});
});
router.post('/dislikeit',function(req,res,next){
	mysql.removeRelation(req.body.schoolid,req.body.id,function(){
	});
});
router.post('/score',function(req,res,next){
	mysql.addStar(req.body.resid,req.body.scores,function(){
		//console.log('111')
	});
	mysql.andStar(req.body.id,req.body.resid,req.body.scores,function(){
	});
});
router.post('/findscore',function(req,res,next){
	mysql.findStar(req.body.id,req.body.resid,function(result){
		res.json(result);
	});
});
router.post('/admin',function(req,res,next){
	switch(req.body.type){
		case '1':mysql.getUser(function(result){
			res.json(result);
		});break;
		case '2':mysql.getAllres(function(result){
			res.json(result);
		});break;
		case '3':mysql.getFeedBakc(function(result){
			res.json(result);
		});break;
	}
});
router.post('/root',function(req,res,next){
	mysql.root(req.body.id,function(){
		res.json({});
	});
});
router.post('/delete',function(req,res,next){
	switch(req.body.type){
		case '1':mysql.deleteUser(req.body.id,function(){
			res.json({});
		});break;
		case '2':mysql.deleteRes(req.body.id,function(result){
			res.json({});
		});break;
		case '3':mysql.deleteFeedBack(req.body.id,function(result){
			res.json({});
		});break;
	}
});
router.post('/addHelp',function(req,res,next){
	mysql.addHelp(req.body.header,req.body.content,function(){
		res.json({});
	});
});
router.post('/getHelp',function(req,res,next){
	mysql.getHelp(function(result){
		res.json(result);
	});
});
module.exports = router;